why my code still cannot connect with database? [closed]

Posted by Wen Teng on Programmers See other posts from Programmers or by Wen Teng
Published on 2012-11-26T14:47:26Z Indexed on 2012/11/26 17:26 UTC
Read the original article Hit count: 202

Filed under:
|
|
package com.mems.travis;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class UserRegister extends Activity {

    JSONParser jsonParser = new JSONParser();
    EditText inputName;
    EditText inputUsername;
    EditText inputEmail;
    EditText inputPassword;
    RadioButton button1;
    RadioButton button2;
    Button button3;
    int success = 0;

    // url to create new product
    private static String url_register_user = "http://192.168.1.100/MEMS/add_user.php";

    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_register);

        // Edit Text
        inputName = (EditText) findViewById(R.id.nameTextBox);
        inputUsername = (EditText) findViewById(R.id.usernameTextBox);
        inputEmail = (EditText) findViewById(R.id.emailTextBox);
        inputPassword = (EditText) findViewById(R.id.pwTextBox);

        // Create button
        //RadioButton button1 = (RadioButton) findViewById(R.id.studButton);
        // RadioButton button2 = (RadioButton) findViewById(R.id.shopownerButton);
        Button button3 = (Button) findViewById(R.id.regSubmitButton);

        // button click event
        button3.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {

                String name = inputName.getText().toString();
                String username = inputUsername.getText().toString();
                String email = inputEmail.getText().toString();
                String password = inputPassword.getText().toString();

                if (name.contentEquals("")||username.contentEquals("")||email.contentEquals("")||password.contentEquals(""))
                {
                    AlertDialog.Builder builder = new AlertDialog.Builder(UserRegister.this);

                    // 2. Chain together various setter methods to set the dialog characteristics
                    builder.setMessage(R.string.nullAlert)
                       .setTitle(R.string.alertTitle);

                    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           // User clicked OK button
                       }
                   });

                // 3. Get the AlertDialog from create()
                AlertDialog dialog = builder.show();
                }
                else
                {
                    new RegisterNewUser().execute();
                }

            }
        });
    }


   class RegisterNewUser extends AsyncTask<String, String, String>{
       protected String doInBackground(String... args) {
           String name = inputName.getText().toString();
           String username = inputUsername.getText().toString();
           String email = inputEmail.getText().toString();
           String password = inputPassword.getText().toString();

           // Building Parameters
           List<NameValuePair> params = new ArrayList<NameValuePair>();
           params.add(new BasicNameValuePair("name", name));
           params.add(new BasicNameValuePair("username", username));
           params.add(new BasicNameValuePair("email", email));
           params.add(new BasicNameValuePair("password", password));

           // getting JSON Object
           // Note that create product url accepts POST method
           JSONObject json = jsonParser.makeHttpRequest(url_register_user,
                               "GET", params);
                       // check log cat for response
           Log.d("Send Notification", json.toString());

           try
               {
                       int success = json.getInt(TAG_SUCCESS);

                       if (success == 1)
                       {
                           // successfully created product
                        Intent i = new Intent(getApplicationContext(), StudentLogin.class);
                        startActivity(i);
                        finish();
                       }

                       else
                       {
                           // failed to register

                       }
               }

                catch (Exception e)
                {
                      e.printStackTrace();
                }
                return null;
               }
   }
}

© Programmers or respective owner

Related posts about java

Related posts about php